<?php
namespace Modules\ShopApi\Models;
use Modules\Admin\Models\BaseModel; 
use Illuminate\Support\Facades\Cache;
class BaseApiModel extends BaseModel
{	
	/**
	 * @name  api成功返回
	 * @param data 	返回信息
	 * @return 返回json数据
	 */
	 public function api_success($data=[]){
		 if(!isset($data['code'])){
			 $data['code'] = 8888;
		 }
		 if(!isset($data['msg'])){
			 $data['msg'] = '成功!';
		 }
		 return response()->json($data,200);
	 }
	 /**
	 * @name  api失败返回
	 * @param data 	返回信息
	 * @return 返回json数据
	 */
	 public function api_error($data=[]){
		 if(!isset($data['code'])){
			 $data['code'] = 4444;
		 }
		 if(!isset($data['msg'])){
			 $data['msg'] = '失败！';
		 }
		 return response()->json($data,400);
	 }
	/**
	 * @name  图片数据格式化 
	 */
	protected function image_list($data){
		$http = getHttpType();	 	
		foreach($data as $k=>$v){
			if($v['image_one']['open'] == 1){
				$data[$k]['image_url'] = $http . $v['image_one']['url'];
			}else{
				$data[$k]['image_url'] = $v['image_one']['url'];
			}
			unset($data[$k]['image_id'],$data[$k]['image_one']);
		}
		return $data;
	}
	/**
	 * @name  公共的发送验证码
	 * @param data 	返回信息
	 * @return 返回json数据
	 */
	public function send_code($phone){
		return $this->api_success([
			'msg'=>'验证码为123456'
		]);
		$code = mt_rand(100000,999999);
		$statusStr = array(
			"0" => "短信发送成功",
			"-1" => "参数不全",
			"-2" => "服务器空间不支持,请确认支持curl或者fsocket，联系您的空间商解决或者更换空间！",
			"30" => "密码错误",
			"40" => "账号不存在",
			"41" => "余额不足",
			"42" => "帐户已过期",
			"43" => "IP地址限制",
			"50" => "内容含有敏感词"
		);
		$smsapi = "http://api.smsbao.com/";
		$user = config('shopapi.dxb_user'); //短信平台帐号
		$pass = md5(config('shopapi.dxb_pass')); //短信平台密码
		$content= config('shopapi.dxb_sign') . "您的验证码为".$code."，在10分钟内有效。";//要发送的短信内容
		$sendurl = $smsapi."sms?u=".$user."&p=".$pass."&m=".$phone."&c=".urlencode($content);
		$result =file_get_contents($sendurl);
		if($result == 0){
			Cache::put('wine_shop_api_'.$phone,$code,600);
			return $this->api_success([
				'msg'=>$statusStr[$result]
			]);
		}else{
			return $this->api_error([
				'msg'=>$statusStr[$result]
			]);
		}
	 }
	/**
	 * @name  公共的验证码验证
	 * @param data 	返回信息
	 * @return 返回json数据
	 */
	public function verify_code($phone,$code){
		if($code == '123456'){   //以后要删除的代码
			return true;
		};
		$v_code = Cache::get('wine_shop_api_'.$phone);
		if($v_code == $code){
			return true;
		}else{
			return false;
		}
	 } 
	 
}
